<?php

function recursive_directory_size($directory, $format=FALSE)
{
    $size = 0;

    // if the path has a slash at the end we remove it here
    if(substr($directory,-1) == '/')
    {
	$directory = substr($directory,0,-1);
    }

    // if the path is not valid or is not a directory ...
    if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory))
    {
	// ... we return -1 and exit the function
	return -1;
    }
    // we open the directory
    if($handle = opendir($directory))
    {
	// and scan through the items inside
	while(($file = readdir($handle)) !== false)
	{
	    // we build the new path
	    $path = $directory.'/'.$file;

	    // if the filepointer is not the current directory
	    // or the parent directory
	    if($file != '.' && $file != '..')
	    {
		// if the new path is a file
		if(is_file($path))
		{
		    // we add the filesize to the total size
		    $size += filesize($path);

		    // if the new path is a directory
		}elseif(is_dir($path))
		    {
			// we call this function with the new path
			$handlesize = recursive_directory_size($path);

			// if the function returns more than zero
			if($handlesize >= 0)
			{
			    // we add the result to the total size
			    $size += $handlesize;

			    // else we return -1 and exit the function
			}else{
			    return -1;
			}
		    }
	    }
	}
	// close the directory
	closedir($handle);
    }
    // if the format is set to human readable
    if($format == TRUE)
    {
	// if the total size is bigger than 1 MB
	if($size / 1048576 > 1)
	{
	    return round($size / 1048576, 1).' MB';

	    // if the total size is bigger than 1 KB
	}elseif($size / 1024 > 1)
	    {
		return round($size / 1024, 1).' KB';

		// else return the filesize in bytes
	    }else{
		return round($size, 1).' bytes';
	    }
    }else{
	// return the total filesize in bytes
	return $size;
    }
}

function log_message($message,$message2="")
{
	if (stristr(PHP_OS, 'WIN')) $logpath=Cabbie::$approot."\log\\"; else $logpath = Cabbie::$approot."/log/";
	$logpath = Cabbie::$approot."/log/";
	if(!($fp = fopen($logpath."cabbie.log", "a+")))
		return false;
	if($message2 == "") fwrite($fp, var_export($message,1)."\n");
	else fwrite($fp, var_export($message.var_export($message2,1),1)."\n");
	fflush($fp);
	fclose($fp);
	return true;
}

function getSession(){

	$params = get_uri_obj();
	$mc = new RemoteCache();
	if(($params->key != "")) {
		$session_data = $mc->get($params->key);
		$data = unserialize (base64_decode($session_data));
		if(substr(Crypt::decrypt($params->key),13) != $data->random) {
			echo "1 )Tampered data, exit now!<br />";
			exit;
		}
		$mc->delete($params->key);
		Cabbie::$_SESSION = $data->sessiondata;
		Cabbie::$_SESSION["sessie_cLogin"]["local"] = 1;
		Cabbie::$_SESSION["sessie_cLogin"]["nbriefid"] = $data->nbriefid;
		$newkey=Crypt::encrypt($_SESSION["sessie_cLogin"]['klantid'].",sessionhybridfootprint,".$data->nbriefid.",".$_SESSION["sessie_cLogin"]["userid"].":".$data->random);
		Cabbie::$_SESSION['id']=$newkey;
		$mc->set($newkey,Cabbie::$_SESSION);
	} else {
		if($params->id != "") {
			$session_data = $mc->get($params->id);
			$data = $session_data;
			Cabbie::$_SESSION=$data;
		} else Cabbie::$_SESSION['id']=session_id();
	}
}

function array_delete($array, $index)
{
    $res = array();

    if(is_numeric($index))
    {
	$i = 0;
	foreach ($array as $item)
	{
	    if ($i!=$index)
	      $res[]=$item;
	    $i++;
	}
	return $res;
    } else if(array_key_exists($index, $array))
	{
	    while(list($key, $val) = each($array))
	    {
		if($key != $index)
		{
		    $res[$key] = $val;
		}
	    }
	    return $res;
	}

    return $array;
}

?>